<template>
  <div>
        <div class="header">
            <span
            class="iconSearch mui-icon mui-icon-search"
            :class="blurFlag? 'mui-icon-search': 'mui-icon-arrowleft'"
            @click="searchIcon"></span>
            <input ref="searchmsg"
            id='search'
            type="text"
            v-model="value"
            :placeholder='`${placeholder}`'
            @keyup.enter='addFriend'
            class="text"
            @focus="searchFocus"
            @blur="searchBlur"
            autocomplete="off">
            <span class="close mui-icon mui-icon-close" @click="closeFun" ref="closeValue" :class="{active: isDelete}"></span>
            <span class="cancel-text" @click="cancel" ref="cancel" :class="{active: isClose}">取消</span>
        </div>
  </div>
</template>
<script>
// import mui from '../../../lib/mui/js/mui.min.js'
export default {
  props: ['placeholder', 'action', 'focus', 'checkflag'],
  data() {
    return {
      // 用户搜索好友所输入的内容
      value: '',
        // 0代表热门搜索 1代表搜索结果 2代表搜索的历史记录
      // toggleFlag: 0,
      // 0表示不出现取消字体，1表示出现取消
      closeFlag: 0,
      // 失去焦点标志 false代表没有失去焦点
      blurFlag: true,
      // 展现验证信息的标志
      checkFlag: false
    }
  },
  created() {
      this.init()
    //   console.log(this.placeholder, this.action, '000000000000000')
  },
  computed: {
    isDelete() {
      if (this.blurFlag) {
          return false
      } else {
          return true
      }
    },
    isClose() {
      if (this.closeFlag) {
        return true
      } else {
        return false
      }
    }
  },
  watch: {
    value(newVal) {
      this.$emit('fucallbackValue', newVal)
    },
    checkflag(newVal) {
      if (!newVal) {
        this.value = ''
      }
    },
    focus(newVal) {
      // this
      // console.log(newVal, 'pppppppppppppp')
      let edit = document.getElementById('search')
      if (newVal) {
        edit.focus()
      } else {
        edit.blur()
      }
    }
  },
  methods: {
    init() {
      var self = this
      self.$immui.init()
    },
    searchIcon() {
    },
  // 点击隐藏验证信息的填写
    keyupFun() {
    },
// 点击之后填写好友验证信息
    addFriend() {
      if (this.action === '添加好友') {
          this.$emit('fucallbackCheckflag', true)
      }
    },
    searchFocus() {
      var self = this
      this.closeFlag = 1
      this.blurFlag = false
      this.$emit('fucallbackBlurflag', self.blurFlag)
    },
    searchBlur() {
      this.closeFlag = 0
      this.blurFlag = true
      // this.value = ''
      this.$emit('fucallbackBlurflag', this.blurFlag)
      // setTimeout
    },
    // 输入错误时，清空内容，还想继续输入时点击X
    closeFun() {
      this.value = ''
      //   点击之后，使得输入款获取焦点
      this.$refs.searchmsg.focus()
  // this.toggleFlag = 2
    },
    // 输入一半时，不想输入时，点击取消
    cancel() {
      this.value = ''
      this.closeFlag = 0
      // this.toggleFlag = 0
    }
  }
}
</script>
<style lang='scss' scoped>
 .header{
    width: 100%;
    height: 52px;
    // background: #f4f4f4;
    display: flex;
    position: relative;
    z-index: 12;
    .iconSearch{
      position: absolute;
      left: 13px;
      top: 12px;
    }
    .text{
      flex: 1;
      display: inline-block;
      width: 80%;
      height: 30px;
      line-height: 30px;
      margin: 10px 10px 0 10px;
      padding-left:26px ;
      outline: none;
      border: 0;
      padding-left: 36px;
      &::-webkit-input-placeholder { /* WebKit browsers */
        color: #ccc;
        font-size: 14px;
        }

      &::-moz-placeholder { /* Mozilla Firefox 4 to 18 */
      color: #ccc;
      font-size: 14px;
        }
      &::-ms-input-placeholder { /* Internet Explorer 10+ */
      color: #ccc;
      font-size: 14px;
        }
    }
    .close {
      position: absolute;
      right: 60px;
      top: 14px;
      font-size: 22px;
      display: none;
      &.active{
        display: block;
      }
    }
    .cancel-text{
      flex: 0 0 40px;
      line-height: 52px;
      padding-right: 4px;
      display: none;
      &.active{
        display: block;
        font-size: 14px;
        color: blue;
      }
    }
  }
  .in-result{
      display: none;
      line-height: 40px;
      padding: 10px;
      box-sizing: border-box;
      &.active{
          display: block;
          background: #f4f4f4;
      }
      .user-search{
          width: 40px;
          height: 40px;
          background: rgb(125, 201, 12);
          text-align: center;
          line-height: 40px;
      }
      .in-text {
          color: rgb(125, 201, 12);
      }
  }
  .check-msg{
      position: fixed;
      top: 0;
      left: 0;
      bottom: 0;
      right: 0;
      background:white;
      z-index: 1000;
    .back-check{
        height: 44px;
        background: #ccc;
        line-height: 44px;
    }
    .add-friend{
        height: 44px;
        line-height: 44px;
        font-size: 22px;
        color: rgb(99, 83, 83);
      }
    .add-text{
        .add-check{
          display: inline-block;
          height: 36px;
          line-height: 36px;
          color: darkgray;
        }
        textarea{
          width: 94vw;
          margin: 0 3vw 3vw 3vw;
          box-sizing: border-box;
        }
      }
      .btn{
        width: 100vw;
        button{
          width: 100vw;
          font-size: 18px;
        }
      }
  }
</style>
